package com.g7.dsp.forprotocol.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import com.g7.dsp.forprotocol.contexts.ApplicationContext;
import com.g7.dsp.forprotocol.contexts.ProtocolContext;
import com.g7.dsp.forprotocol.services.Converter;
import com.g7.dsp.forprotocol.services.impl.LiquidConverter;
import com.g7.dsp.forprotocol.ui.components.G7JButton;

public class SmartGui implements ActionListener,ItemListener  {
	public static final String[] SMART_LABELS= {"..........请选择..........", "油感", "其他"};
	// 页面布局对象
	JTextField ja1;
	JTextField ja2;
	JFileChooser fd;
	JFrame jf;
//	JPanel j1;
	JPanel j2;
	JPanel j3;
	G7JButton jb1;
	G7JButton jb3;
	G7JButton jb2;
	JLabel jl1;
	JLabel lblNewLabel;
	JLabel lblOutputDir;
	JComboBox protoColcomboBox;

	//应用交互对象
	private HashMap<String,Converter>strategyMap;
	ApplicationContext app;
	ProtocolContext prt;
	private String selectedProtocol = null;
	private String outputFileStr = null;
	
	
	public SmartGui(JFrame mainFrame) {
		this.app = ApplicationContext.getApplicationContext();
		this.prt = ProtocolContext.getProtocolContext();
		initStrategy();
		this.jf = mainFrame;
//		this.j1 = new JPanel();
		this.j2 = new JPanel();
		this.j3 = new JPanel();
		this.jl1 = new JLabel("源文件:");
		jl1.setBounds(10, 13, 68, 18);
		jl1.setForeground(Color.BLACK);
		jl1.setVerticalAlignment(SwingConstants.BOTTOM);
		jl1.setHorizontalAlignment(SwingConstants.TRAILING);
		this.jb1 = new G7JButton(ProtocolContext.SELECT_LABEL);
		this.jb2 = new G7JButton(ProtocolContext.SELECT_LABEL);
		jb2.setBounds(294, 84, 63, 27);
		jb1.setBounds(294, 9, 63, 27);
		this.jb3 = new G7JButton(ProtocolContext.CONVERT);
		this.jb1.addActionListener(this);
		this.jb2.addActionListener(this);
		this.jb3.addActionListener(this);
//		j1.setLayout(new FlowLayout(FlowLayout.LEFT, 5, 5));
//		j1.add(jl1);
		this.ja1 =  new JTextField(20);
		this.ja1.setBounds(92, 10, 200, 24);
		this.ja1.setEditable(false);
		this.ja1.setBackground(Color.WHITE);
		this.ja2 =  new JTextField(20);
		this.ja2.setBounds(92, 84, 200, 24);
		this.ja2.setEditable(false);
		this.ja2.setBackground(Color.WHITE);
//		j1.add(this.ja1);
//		j1.add(this.jb1);
		this.jb3.setEnabled(false);
		j2.add(this.jb3);
		lblNewLabel = new JLabel("选择协议:");
		lblNewLabel.setBounds(10, 50, 68, 18);
		lblNewLabel.setForeground(Color.BLACK);
		lblNewLabel.setVerticalAlignment(SwingConstants.BOTTOM);
		lblNewLabel.setHorizontalAlignment(SwingConstants.TRAILING);
		lblOutputDir = new JLabel("输出目录:");
		lblOutputDir.setBounds(10, 87, 68, 18);
		lblOutputDir.setForeground(Color.BLACK);
		lblOutputDir.setVerticalAlignment(SwingConstants.BOTTOM);
		lblOutputDir.setHorizontalAlignment(SwingConstants.TRAILING);
		protoColcomboBox = new JComboBox(SMART_LABELS);
		protoColcomboBox.setBounds(92, 47, 265, 24);
		protoColcomboBox.setEditable(true);
		protoColcomboBox.addItemListener(this);
		j3.setLayout(null);
		j3.add(jl1);
		j3.add(ja1);
		j3.add(jb1);
		j3.add(lblNewLabel);
		j3.add(protoColcomboBox);
		j3.add(lblOutputDir);
		j3.add(ja2);
		j3.add(jb2);
		
        app.initUI(jf);
//		this.jf.getContentPane().add(j1, BorderLayout.NORTH);
		this.jf.getContentPane().add(j2, BorderLayout.SOUTH);
		this.jf.getContentPane().add(j3, BorderLayout.CENTER);
//        prt.getPanelList().add(j1);
		app.getPanelList().add(j2);
		app.getPanelList().add(j3);
        
        this.jf.setSize(400, 400);
		this.jf.setVisible(true);
//		this.jf.pack();
	}


	public void actionPerformed(ActionEvent e) {
		if (e.getSource()==jb1) {
			fd = new JFileChooser();
			fd.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
			fd.showDialog(new JLabel(), "选择");
			File file = fd.getSelectedFile();
			String filePath = file.getAbsolutePath();
			String fileName = file.getName();
			if (fileName == null || fileName.equals("")) {
				JOptionPane.showMessageDialog(this.ja1, "文件错误请重新选择支持的协议文件！");
				return;
			}
			// 遍历文件夹下面的报文文件
			List<String> fileList = new ArrayList<String>();
			this.ja1.setText(filePath);
			if (file.isDirectory()) {
				ergodic(file, fileList);
			} else {
				forSingleFile(file, fileList);
			}
			
			if (
                prt.getErrorFileCount() > 0) {
				JOptionPane.showMessageDialog(this.ja1, "文件中存在不规范的文件名，已选择合规文件！");
			}
			if (fileList.size() > 0) {
				prt.setFileList(fileList);
			} else {
				JOptionPane.showMessageDialog(this.ja1, "未选中合法的协议文本文件，请重新选择！");
			}
		} else if (e.getSource()==jb2) {
			fd = new JFileChooser();
			fd.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			fd.showDialog(new JLabel(), "选择");
			File file = fd.getSelectedFile();
			String filePath = file.getAbsolutePath();
			String fileName = file.getName();
			if (fileName == null || fileName.equals("")) {
				JOptionPane.showMessageDialog(this.ja1, "请重新选择支持的文件夹！");
				return;
			}
			prt.setOutputFilePath(filePath);
			this.ja2.setText(filePath);
			if(prt.getFileList()!=null&&prt.getFileList().size()>0){
				this.jb3.setEnabled(true);
			}
		}
		else if (e.getSource()==jb3) {
			if (!selectedProtocol.equals(SMART_LABELS[0])) {
				strategyMap.get(selectedProtocol).processConvert();
			}
			this.jb1.setEnabled(false);
			this.jb2.setEnabled(false);
			this.jb3.setEnabled(false);
		} else {
			JOptionPane.showMessageDialog(this.ja1, "请选择适合的协议！");
			return;
		}
	}

	private void ergodic(File file, List<String> resultFileName) {
		File[] files = file.listFiles();
		if (files == null)
			return;// 判断目录下是不是空的
		for (File f : files) {
			if (f.isDirectory()) {// 判断是否文件夹
				ergodic(f, resultFileName);// 调用自身,查找子目录
			} else {
				forSingleFile(f, resultFileName);
			}
		}
	}

	private void forSingleFile(File file, List<String> resultFileName) {
		try {
			Integer.valueOf(file.getName());
			resultFileName.add(file.getAbsolutePath());
		} catch (NumberFormatException e1) {
			prt.getAndIncreErrorCount();
		}
	}
	
	
	@Override
	public void itemStateChanged(ItemEvent e) {
		// TODO Auto-generated method stub
		if (e.getStateChange() == ItemEvent.SELECTED) {
			selectedProtocol = (String) protoColcomboBox.getSelectedItem();
		}
	}
	
	private void initStrategy(){
		strategyMap = new HashMap<String,Converter>();
		strategyMap.put("油感", new LiquidConverter());
	}
}
